﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UnityEngine.UI
{
    public class SDParticlePool : MonoBehaviour
    {
        private static SDParticlePool _instance;
        public static SDParticlePool Instance
        {
            get
            {
                if (_instance == null)
                {
                    Init();
                }
                return _instance;
            }
        }
        private List<SDEffectSprite> m_Pool = new List<SDEffectSprite>();

        public static void Init()
        {
            if (_instance == null)
            {
                GameObject go = new GameObject("~SDParticlePool");
                _instance = go.AddComponent<SDParticlePool>();
                DontDestroyOnLoad(go);
            }
        }

        public SDEffectSprite Create()
        {
            SDEffectSprite effectSprite;
            if (m_Pool.Count > 0)
            {
                effectSprite = m_Pool[m_Pool.Count - 1];
                m_Pool.RemoveAt(m_Pool.Count - 1);
                return effectSprite;
            }
            GameObject go = new GameObject("SDParticle");
            effectSprite = go.AddComponent<SDEffectSprite>();
            return effectSprite;
        }

        public void Recycle(SDEffectSprite effectSprite)
        {
            effectSprite.transform.parent = transform;
            effectSprite.Clear();
            m_Pool.Add(effectSprite);
        }
    }
}
